package com.fingard.avatar.website.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fingard.avatar.website.dao.SysUserMapper;
import com.fingard.avatar.website.exception.BizException;
import com.fingard.avatar.website.model.dto.IdDto;
import com.fingard.avatar.website.model.dto.UserAddDto;
import com.fingard.avatar.website.model.dto.UserPageDto;
import com.fingard.avatar.website.model.enums.DeletedEnum;
import com.fingard.avatar.website.model.enums.RoleEnum;
import com.fingard.avatar.website.model.po.SysUserPo;
import com.fingard.avatar.website.service.SysUserService;
import com.fingard.avatar.website.utils.DaoUtils;
import lombok.extern.slf4j.Slf4j;
import org.casbin.jcasbin.main.Enforcer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *  Service实现类
 *
 * @author 徐一涛
 * @date 2022-04-26
 */
@Slf4j
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserPo> implements SysUserService {

    @Resource
    protected Enforcer enforcer;

    @Override
    public Boolean add(UserAddDto req) {
        SysUserPo user = getOne(Wrappers.<SysUserPo>lambdaQuery().
                eq(SysUserPo::getUserName, req.getUserName()).
                eq(SysUserPo::getIsDeleted, DeletedEnum.NOT_DELETED.getValue()));
        if (ObjectUtil.isNotNull(user)) {
            throw BizException.of("用户名已存在，请重新命名");
        }

        String mdPwd = SecureUtil.md5(req.getPassword());
        user = new SysUserPo().setUserName(req.getUserName()).setPassword(mdPwd).setRole(RoleEnum.USER.name());

        DaoUtils.insert(user);
        save(user);
        enforcer.addRoleForUser(user.getUserName(),RoleEnum.USER.name());
        return true;
    }

    @Override
    public Boolean delete(IdDto req) {
        SysUserPo user = getOne(Wrappers.<SysUserPo>lambdaQuery().
                eq(SysUserPo::getId, req.getId()).
                eq(SysUserPo::getIsDeleted, DeletedEnum.NOT_DELETED.getValue()));
        if (ObjectUtil.isNull(user)) {
            throw BizException.of("用户不存在");
        }
        DaoUtils.delete(user);
        updateById(user);
        return true;
    }

    @Override
    public IPage<SysUserPo> page(UserPageDto req) {
        LambdaQueryWrapper<SysUserPo> wrapper = Wrappers.<SysUserPo>lambdaQuery().
                eq(SysUserPo::getIsDeleted,DeletedEnum.NOT_DELETED.getValue()).
                orderByDesc(SysUserPo::getCreateTime);
        if(StrUtil.isNotEmpty(req.getUserName())) {
            wrapper.like(SysUserPo::getUserName, req.getUserName());
        }
        IPage<SysUserPo> iPage = new Page<>(req.getPageNum(), req.getPageSize());
        return page(iPage, wrapper);
    }
}
